﻿namespace HZY.Framework.Repository.EntityFramework.Interceptor.Audit;

/// <summary>
/// FreeSql审计拦击器
/// </summary>
public abstract class AbstractFreeSqlAuditAop
{
    /// <summary>
    /// 获取当前用户
    /// </summary>
    /// <returns></returns>
    protected abstract string? GetCurrentUserId();

    /// <summary>
    /// 获取雪花 id
    /// </summary>
    /// <returns></returns>
    protected abstract long GetSnowflakeId();

    /// <summary>
    /// 审计
    /// </summary>
    /// <param name="auditInfo"></param>
    /// <param name="s"></param>
    /// <param name="serviceProvider"></param>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public virtual void OnAudit(AuditValueEventArgs auditInfo, object? s, IServiceProvider? serviceProvider)
    {
        var tableIdAttribute = TableIdAttribute.GetTableIdAttribute(auditInfo.Property);
        tableIdAttribute?.HandleByFreeSql(auditInfo, GetSnowflakeId(), serviceProvider);

        var tableFieldAttribute = TableFieldAttribute.GetTableFieldAttribute(auditInfo.Property);
        tableFieldAttribute?.HandleFillByFreeSql(auditInfo, GetCurrentUserId());
    }
}